<?php

namespace App\Admin\Controllers\Statistics;

use App\Admin\Controllers\AdminController;
use App\Admin\Extensions\Tools\TypeSelect;
use App\Admin\Repositories\Statistics\SalePersonalRepository;
use App\Enums\DataPermission;
use App\Enums\OptionType;
use App\Models\Department;
use App\Models\Ding;
use App\Services\OptionService;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;

class SalePersonalController extends AdminController
{
    public bool $disable_pagination = true;
    public bool $disable_actions = true;
    public bool $create = false;

    public function index(Content $content): Content
    {
        return $content
            ->title(trans('statistics.labels.statisticsSalePersonal'))
            ->body($this->grid());
    }

    protected function grid(): Grid
    {
        $data_permission = $this->getAdminDataPermission();
        return Grid::make(new SalePersonalRepository(), function (Grid $grid) use ($data_permission) {
            $grid->column('date_time', trans('statistics.fields.date_time'))->sortable();
            $grid->column('user_name', trans('statistics.fields.user_name'));
            $grid->column('count_enter', trans('statistics.fields.count_enter'))->sortable();
            $grid->column('count_reception', trans('statistics.fields.count_reception'))->sortable();
            $grid->column('count_sale', trans('statistics.fields.count_sale'))->sortable();
            $grid->column('count_sale_timely', trans('statistics.fields.count_sale_timely'))->sortable();
            $grid->column('count_loss', trans('statistics.fields.count_loss'))->sortable();
            $grid->column('rate_loss', trans('statistics.fields.rate_loss'))
                ->help(trans('statistics.fields.count_loss') . ' / ' . trans('statistics.fields.count_enter') . '  * 100%')
                ->display(function ($value) {
                    return $value . '%';
                })->sortable();
            $grid->column('rate_timely_enter', trans('statistics.fields.rate_timely_enter'))
                ->help(trans('statistics.fields.count_sale_timely') . ' / ' . trans('statistics.fields.count_enter') . '  * 100%')
                ->display(function ($value) {
                    return $value . '%';
                })->sortable();
            $grid->column('rate_timely_sale', trans('statistics.fields.rate_timely_sale'))
                ->help(trans('statistics.fields.count_sale_timely') . ' / ' . trans('statistics.fields.count_sale') . '  * 100%')
                ->display(function ($value) {
                    return $value . '%';
                })->sortable();
            $grid->column('rate_conversion', trans('statistics.fields.rate_conversion'))
                ->help(trans('statistics.fields.count_sale') . ' / ' . trans('statistics.fields.count_enter') . '  * 100%')
                ->display(function ($value) {
                    return $value . '%';
                })->sortable();
            $grid->column('unit_price', trans('statistics.fields.unit_price'))
                ->help(trans('statistics.fields.money_total') . ' / ' . trans('statistics.fields.count_sale'))->sortable();
            $grid->column('money_total', trans('statistics.fields.money_total'))->sortable();
            $grid->column('arrears_total', trans('statistics.fields.arrears_total'))->sortable();
            $grid->column('cash_back_total', trans('statistics.fields.sale_cash_back_total'))->sortable();
            $grid->column('count_no_deposit', trans('statistics.fields.count_no_deposit'))->sortable();
            $grid->column('count_deposit', trans('statistics.fields.count_deposit'))->sortable();
            $grid->column('rate_full_payment', trans('statistics.fields.rate_full_payment'))
                ->help(trans('statistics.fields.count_full_payment') . ' / ' . trans('statistics.fields.count_enter') . '  * 100%')
                ->display(function ($value) {
                    return $value . '%';
                })->sortable();
//            $grid->column('dispute_money', trans('statistics.fields.dispute_money'))->sortable();
//            $grid->column('dispute_money_unit', trans('statistics.fields.dispute_money_unit'))->sortable();

            $grid->filter(function (Grid\Filter $filter) use ($data_permission) {
                switch (request('type', 'day')) {
                    case 'day':
                        $filter->between('date', trans('admin.time'))->date();
                        break;
                    case 'month':
                        $filter->between('date', trans('admin.time'))->month();
                        break;
                }
                if ($data_permission == DataPermission::ALL) {
                    $filter->like('username', trans('statistics.fields.user_name'));
                    $filter->equal('ding_id', trans('user.fields.ding_id'))->select(Ding::pluck('name', 'id'))->load('dept_id', '/getDept');
                    $filter->equal('dept_id', trans('user.fields.dept_id'))->select();
                }

            });
            $grid->export();
            $grid->export()->disableExportAll();
            $grid->tools(new TypeSelect([
                'day' => '按日查看',
                'month' => '按月查看',
            ]));

            $this->gridCommon($grid);

            Admin::style(
                <<<STYLE
    .table-responsive {
        overflow-x: initial !important;
    }
    .table-responsive thead th {
        position: sticky;
        top: 0;
        background-color: #f8f9fa;
    }
STYLE
            );
        });
    }
}
